package com.ruoyi.sys.security.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.sys.security.mapper.TbIncidentMapper;
import com.ruoyi.sys.security.domain.TbIncident;
import com.ruoyi.sys.security.service.ITbIncidentService;

/**
 * 园区事件记录Service业务层处理
 *
 * @author iuxin
 * @date 2022-05-25
 */
@Service
public class TbIncidentServiceImpl implements ITbIncidentService
{
    @Autowired
    private TbIncidentMapper tbIncidentMapper;

    /**
     * 查询园区事件记录
     *
     * @param iId 园区事件记录主键
     * @return 园区事件记录
     */
    @Override
    public TbIncident selectTbIncidentByIId(Long iId)
    {
        return tbIncidentMapper.selectTbIncidentByIId(iId);
    }

    /**
     * 查询园区事件记录列表
     *
     * @param tbIncident 园区事件记录
     * @return 园区事件记录
     */
    @Override
    public List<TbIncident> selectTbIncidentList(TbIncident tbIncident)
    {
        return tbIncidentMapper.selectTbIncidentList(tbIncident);
    }

    /**
     * 新增园区事件记录
     *
     * @param tbIncident 园区事件记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbIncident(TbIncident tbIncident)
    {
        int rows = tbIncidentMapper.insertTbIncident(tbIncident);
        insertSysUser(tbIncident);
        return rows;
    }

    /**
     * 修改园区事件记录
     *
     * @param tbIncident 园区事件记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbIncident(TbIncident tbIncident)
    {
        tbIncidentMapper.deleteSysUserByUserId(tbIncident.getiId());
        insertSysUser(tbIncident);
        return tbIncidentMapper.updateTbIncident(tbIncident);
    }

    /**
     * 批量删除园区事件记录
     *
     * @param iIds 需要删除的园区事件记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbIncidentByIIds(Long[] iIds)
    {
        tbIncidentMapper.deleteSysUserByUserIds(iIds);
        return tbIncidentMapper.deleteTbIncidentByIIds(iIds);
    }

    /**
     * 删除园区事件记录信息
     *
     * @param iId 园区事件记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbIncidentByIId(Long iId)
    {
        tbIncidentMapper.deleteSysUserByUserId(iId);
        return tbIncidentMapper.deleteTbIncidentByIId(iId);
    }

    /**
     * 新增用户信息信息
     *
     * @param tbIncident 园区事件记录对象
     */
    public void insertSysUser(TbIncident tbIncident)
    {
        List<SysUser> sysUserList = tbIncident.getSysUserList();
        Long iId = tbIncident.getiId();
        if (StringUtils.isNotNull(sysUserList))
        {
            List<SysUser> list = new ArrayList<SysUser>();
            for (SysUser sysUser : sysUserList)
            {
                sysUser.setUserId(iId);
                list.add(sysUser);
            }
            if (list.size() > 0)
            {
                tbIncidentMapper.batchSysUser(list);
            }
        }
    }
}
